import 'dart:ffi';

import 'package:cher_flare/config/config.dart';
import 'package:cher_flare/routes/app_pages.dart';
import 'package:cher_flare/utils/keyboard_util.dart';
import 'package:cher_flare/utils/ly_app_tracking_transparency.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'config/url_api.dart';
import 'http/http_utils.dart';
import 'utils/size_fit.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Config.init();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    LYAppTrackingTransparency.share.init();
  }

  @override
  Widget build(BuildContext context) {
    LYSizeFit.initialize();
    debugPrint('=====initialRoute========${Config.isFirstLaunch}=======');
    final initialRoute = Config.isFirstLaunch
        ? AppPages.guidePage
        : (Config.isLogin ? AppPages.home : AppPages.login);
    return RefreshConfiguration(
      footerBuilder: () => const ClassicFooter(
        loadingText: "加载中...",
        idleText: "下拉加载",
        canLoadingText: "松开开始加载数据",
      ),
      child: GetMaterialApp(
        initialRoute: initialRoute,
        getPages: AppPages.routes,
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
            textTheme: TextTheme(labelLarge: TextStyle(fontSize: 14.px))),
        builder: (context, child) {
          return FlutterEasyLoading(
            child: GestureDetector(
              child: child,
              onTap: () => KeyboardUtils.hideKeyBoard(context),
            ),
          );
        },
      ),
    );
  }
}
